<template>
  <div>
    <el-button type="primary" @click="add">添加</el-button>
    <list-vue :info="info" :arr="arr" @willUpdate="willUpdate" @init="getList"></list-vue >
    <form-vue :info="info" @init="getList" ref="form"></form-vue>
    <el-pagination
      @current-change="changePage"
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="size"
    >
    </el-pagination>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import listVue from "./components/list.vue";
import formVue from "./components/form.vue";
import { reqspecscount, reqspecslist } from '../../http/api';
export default {
  props: [],
  components:{
    listVue,
    formVue
  },
  data() {
    return {
      info:{
        isshow:false,
        isAdd:false
      },
      arr:[],
      total:1,//总条目数
      size: 2, //一页的数量
      page: 1, //当前页码
    };
  },
  computed: {
    ...mapGetters({}),
  },
  mounted() {
    this.getList();
    // this.requestlist()
  },
  methods: {
    ...mapActions({}),
    add(){
      this.info.isshow = true;
      this.info.isAdd = true;
    },
    getList(){
      // 请求列表
      this.requestlist()
      // 请求总数
      reqspecscount().then(res => {
        // console.log('总数',res);
        this.total = res.data.list[0].total;
      })
    },
    requestlist(){
      reqspecslist({size:this.size,page:this.page}).then(res => {
        let arr = res.data.list ? res.data.list : [];
        if(this.page !=1 && arr.length == 0){
          this.page--;
          this.requestlist();
          return;
        }
        this.arr = arr;
      })
    },
    changePage(e){
      this.page = e;
      this.requestlist()
    },
    willUpdate(id){
      this.$refs.form.getOne(id);
      this.info.isshow = true;
      this.info.isAdd = false;
    }
  },
};
</script>
<style scoped lang="less">
@import "../../less/index.less";
.el-button{
  margin: 20px;
}
</style>